/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package com.swak.monitor.dubbo.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;

import com.swak.annotation.Delete;
import com.swak.annotation.Get;
import com.swak.annotation.Post;
import com.swak.annotation.Put;
import com.swak.annotation.RestApi;
import com.swak.monitor.dubbo.exception.ParamValidationException;
import com.swak.monitor.dubbo.exception.ResourceNotFoundException;
import com.swak.monitor.dubbo.model.dto.AccessDTO;
import com.swak.monitor.dubbo.model.dto.ConditionRouteDTO;
import com.swak.monitor.dubbo.service.RouteService;
import com.swak.monitor.dubbo.utils.Constants;
import com.swak.utils.StringUtils;

@RestApi(value = "DubboAccessesApi", path = "/api/monitor/:env/rules/access")
public class AccessesApi {

	@Autowired
	private RouteService routeService;
	// @Autowired
	// private ProviderService providerService;

	@Get
	public List<AccessDTO> searchAccess(String service, String application, String env) {
		if (StringUtils.isBlank(service) && StringUtils.isBlank(application)) {
			throw new ParamValidationException("Either service or application is required");
		}
		List<AccessDTO> accessDTOS = new ArrayList<>();
		AccessDTO accessDTO;
		if (StringUtils.isNotBlank(application)) {
			accessDTO = routeService.findAccess(application);
		} else {
			accessDTO = routeService.findAccess(service);
		}
		if (accessDTO != null) {
			accessDTO.setEnabled(true);
			accessDTOS.add(accessDTO);
		}
		return accessDTOS;
	}

	@Get("/:id")
	public AccessDTO detailAccess(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		AccessDTO accessDTO = routeService.findAccess(id);
		return accessDTO;
	}

	@Delete("/:id")
	public void deleteAccess(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		routeService.deleteAccess(id);
	}

	@Post
	public void createAccess(AccessDTO accessDTO, String env) {
		if (StringUtils.isBlank(accessDTO.getService()) && StringUtils.isBlank(accessDTO.getApplication())) {
			throw new ParamValidationException("Either Service or application is required.");
		}
		// String application = accessDTO.getApplication();
//        if (StringUtils.isNotEmpty(application)
//            && "2.6".equals(providerService.findVersionInApplication(application))) {
//            throw new VersionValidationException("dubbo 2.6 does not support application scope blackwhite list config");
//        }
		if (accessDTO.getBlacklist() == null && accessDTO.getWhitelist() == null) {
			throw new ParamValidationException("One of Blacklist/Whitelist is required.");
		}
		routeService.createAccess(accessDTO);
	}

	@Put("/:id")
	public void updateAccess(String id, AccessDTO accessDTO, String env) {

		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		ConditionRouteDTO route = routeService.findConditionRoute(id);
		if (Objects.isNull(route)) {
			throw new ResourceNotFoundException("Unknown ID!");
		}
		routeService.updateAccess(accessDTO);
	}
}
